package com.cskaoyan.service.impl;


import com.cskaoyan.bean.IssueListData;
import com.cskaoyan.bean.IssueParameter;
import com.cskaoyan.mapper.MarketIssueMapper;
import com.cskaoyan.model.MarketIssue;
import com.cskaoyan.model.MarketIssueExample;
import com.cskaoyan.service.IssueService;

import com.cskaoyan.utils.MyBatisUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.ibatis.session.SqlSession;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class IssueServiceImpl implements IssueService {
    @Override
    public IssueListData getIssue(IssueParameter parameter) {
        //创建IssueListData对象
        IssueListData issueListData = new IssueListData();
        Integer page = parameter.getPage();
        Integer limit = parameter.getLimit();
        String sort = parameter.getSort();
        String order = parameter.getOrder();
        String question = parameter.getQuestion();


        //查询Market_Issue数据库得到List集合
        MarketIssueExample marketIssueExample = new MarketIssueExample();
        MarketIssueExample.Criteria criteria = marketIssueExample.createCriteria().andDeletedEqualTo(false);
        if (question != null){
            criteria.andQuestionLike("%" + question + "%");
        }
        marketIssueExample.setOrderByClause(sort + " " + order);


        SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
        MarketIssueMapper mapper = sqlSession.getMapper(MarketIssueMapper.class);
        PageHelper.startPage(page,limit);
        List<MarketIssue> marketIssues = mapper.selectByExample(marketIssueExample);
        //对数据进行分页处理
        PageInfo pageInfo = new PageInfo(marketIssues);
        issueListData.setList(marketIssues);
        issueListData.setPage(page);
        issueListData.setTotal((int) pageInfo.getTotal());
        issueListData.setLimit(limit);
        issueListData.setPages(pageInfo.getPages());
        sqlSession.commit();
        sqlSession.close();

        return issueListData;
    }

    @Override
    public MarketIssue insertNewIssue(Map<String, String> map) {
        MarketIssue marketIssue = new MarketIssue();
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketIssueMapper mapper = sqlSession.getMapper(MarketIssueMapper.class);

        MarketIssueExample marketIssueExample = new MarketIssueExample();

        marketIssue.setAddTime(new Date());
        marketIssue.setUpdateTime(new Date());
        marketIssue.setAnswer(map.get("answer"));
        marketIssue.setQuestion(map.get("question"));

        mapper.insertSelective(marketIssue);


        sqlSession.commit();
        sqlSession.close();

        return marketIssue;
    }

    @Override
    public void deleteIssue(Map<String, String> map) {
        MarketIssue marketIssue = new MarketIssue();


        ConvertUtils.register(new Converter() {
            @Override
            public <T> T convert(Class<T> aClass, Object o) {
                return null;
            }
        }, Date.class);

        try {
            BeanUtils.copyProperties(marketIssue,map);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
        marketIssue.setDeleted(true);

        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketIssueMapper mapper = sqlSession.getMapper(MarketIssueMapper.class);

        mapper.updateByPrimaryKeySelective(marketIssue);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public MarketIssue updateIssue(Map<String, String> map) {
        MarketIssue marketIssue = new MarketIssue();


        ConvertUtils.register(new Converter() {
            @Override
            public <T> T convert(Class<T> aClass, Object o) {
                return null;
            }
        }, Date.class);

        try {
            BeanUtils.copyProperties(marketIssue,map);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }


        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketIssueMapper mapper = sqlSession.getMapper(MarketIssueMapper.class);


        mapper.updateByPrimaryKeySelective(marketIssue);
        sqlSession.commit();

        sqlSession.close();

        return marketIssue;
    }
}
